package clientapp;


import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.StringTokenizer;

public class DeleteFrame extends JFrame implements ActionListener
{
	private JComboBox cmbDelete;
	private JButton btnDelete = new JButton("Delete");
	private JButton btnCancel = new JButton("Cancel");
		
	String movies[];
	/*depending on which item the user selects in combobox, eg.
	  array-index index 0, which is the the first combobox item, will
	  store the corresponding index in dvd/customers object array
	 */
	
	String movieCode = new String();
	String movieName = new String();
	String displayMovie = new String();
	private Client clientr;

	public DeleteFrame(boolean isDvd, Client clientp)
	{
        clientr = (Client)clientp;
		if(isDvd)//if dvd delete form, load combobox with DVD's
			clientr.sendQuery("DeleteMovie1");
		else
			clientr.sendQuery("DeleteCustomer1");
			
		if(clientr.getResult(0,0).equals("#No Records"))
		{
			runStore.store.setEnabled(true);
			if(isDvd)
			{
				JOptionPane.showMessageDialog(null,"There are no movies to be deleted.");
			}
			else
				JOptionPane.showMessageDialog(null,"There are no customers to be deleted.");
			dispose();
		}
		else
		{
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			setSize(400,150);
			setResizable(false);
			addWindowListener (new WindowAdapter ()
			{
		    	public void windowClosing (WindowEvent e)
		    	{ //overiding windowClosing
					runStore.store.setEnabled(true);
		    	}
			} //the improved WindowListener is added.
			);
			JLabel lblText = new JLabel();
			cmbDelete = new JComboBox();
			loadItems(isDvd, lblText);
			setLocationRelativeTo(null);
			
			//layout
			setLayout(new BorderLayout());
			
			JPanel centerPanel = new JPanel(new FlowLayout());
			centerPanel.add(lblText);
			centerPanel.add(cmbDelete);
			
			JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			bottomPanel.add(btnDelete);
			bottomPanel.add(btnCancel);		
			
			btnDelete.addActionListener(this);
			btnCancel.addActionListener(this);
			add(centerPanel,BorderLayout.CENTER);
			add(bottomPanel,BorderLayout.SOUTH);
			add(new JPanel(), BorderLayout.NORTH);
			
			setVisible(true);
		}
	}
	
	public void loadItems(boolean a, JLabel label)
	{
		if(a)//load dvd items
		{
			setTitle("Delete DVD");
			label.setText("Select DVD to be deleted: ");
			//Client.sendQuery("DeleteMovie1");
			loadDvdCombo();
		}
		else//load customer items
		{
			setTitle("Delete Customer");
			label.setText("Select Customer to be deleted: ");
			//Client.sendQuery("DeleteCustomer1");
			loadCustomerCombo();
		}
	}
	
	private void loadDvdCombo()
	{
		//should be loaded with DVD num and Name
		movies = new String[clientr.getResult().length];
		
		for(int i = 0; i < clientr.getResult().length; i++)
		{
			for(int j = 0; j < 2; j++)
			{
				movieCode = clientr.getResult()[i][0];
				movieName =  clientr.getResult()[i][1];
			}
			
			displayMovie = movieCode + " " + movieName;
			movies[i] = displayMovie;			
		}
		
		cmbDelete = new JComboBox(movies);
	}
	
	private void loadCustomerCombo()
	{
		movies = new String[clientr.getResult().length];
		
		for(int i = 0; i < clientr.getResult().length; i++)
		{
			for(int j = 0; j < 2; j++)
			{
				movieCode = clientr.getResult()[i][0];
				movieName =  clientr.getResult()[i][1];
			}
			
			displayMovie = movieCode + " " + movieName;
			movies[i] = displayMovie;			
		}
		
		cmbDelete = new JComboBox(movies);
	}
	
	/*
	 When a dvd or customer is deleted, a loop is used to move every object down an index
	 until the last object is moved to the place of the selected item.
	 A customer may have many DVD's so if a customer is deleted who still has dvd's
	 taken out, every dvd he has is also deleted.
	 */
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==btnDelete)
		{
			String selected = (String)cmbDelete.getSelectedItem();
					
			
			if(getTitle() == "Delete DVD")//delete dvd
			{
				StringTokenizer first = new StringTokenizer(selected," ");
				String code = first.nextToken();
				
				String sendToServer = "DeleteMovie2" + "#" + code;
				clientr.sendQuery(sendToServer);
				cmbDelete.removeItem(selected);
				
				if(clientr.getResult()[0][0].equals("#OutDvd"))
				{
					int reply = JOptionPane.showConfirmDialog(null, "The DVD you selected is taken out!\nAre you sure you want to delete it?",
					"Delete Dvd", JOptionPane.YES_NO_OPTION);
					
					if(reply == JOptionPane.YES_OPTION)
					{
						String sendToClient = "DeleteMovie3" + "#" + code;
						clientr.sendQuery(sendToClient);
						cmbDelete.removeItem(selected);
					}
				}
				if(cmbDelete.getItemCount() == 0)
				{
					runStore.store.setEnabled(true);
					JOptionPane.showMessageDialog(null,"The last DVD has been deleted.");
					dispose();
				}
			}//end delete DVD
			else//delete customer
			{
				StringTokenizer first = new StringTokenizer(selected," ");
				String number = first.nextToken();
				
				String sendToServer = "DeleteCustomer2" + "#" + number;
				clientr.sendQuery(sendToServer);
				cmbDelete.removeItem(selected);
				
				if(clientr.getResult()[0][0].equals("#hasDvd"))
				{
					int reply = JOptionPane.showConfirmDialog(null, 
					"The Customer you selected has DVD(s) taken out!\nAre you sure you want to delete him/her?"	+ "\nWarning: deleting this customer will also delete the DVD(s) he or she has!",
					"Delete Customer", JOptionPane.YES_NO_OPTION);
					
					if(reply == JOptionPane.YES_OPTION)
					{
						String sendToClient = "DeleteCustomer3" + "#" + number;
						clientr.sendQuery(sendToClient);
						cmbDelete.removeItem(selected);
					}
				}
				//lastly check if there are items to be deleted else close form
				if(cmbDelete.getItemCount() == 0)
				{
					runStore.store.setEnabled(true);
					JOptionPane.showMessageDialog(null,"The last customer has been deleted.");
					dispose();
				}
			}
		}
		else if(e.getSource()==btnCancel)
		{
			//runStore.store.setEnabled(true);
			dispose();
		}
	}
	
	private void deleteOneDvd(int dvdIndex)
	{
		
	}
}
